package org.example;

import io.github.bonigarcia.wdm.WebDriverManager;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class FirstTest {
    WebDriver driver=null;

    void createDriver() throws InterruptedException {

        WebDriverManager.edgedriver().setup();
        EdgeOptions options = new EdgeOptions();
        options.addArguments("--remote-allow-origins=*");
        driver = new EdgeDriver(options);
        Thread.sleep(3000);
        driver.get("https://www.baidu.com");
        Thread.sleep(3000);
    }
    //测试百度搜索关键词  范冰冰
    //打开浏览器
    //输入完整的网页信息
    //找到输入框，输入关键字
    //找到百度一下按钮，点击
    //关闭浏览器
    void  tset1() throws InterruptedException {
        //1打开浏览器，使用驱动来打开
//        WebDriverManager.chromedriver().setup();
//        //增加浏览器驱动配置，创建驱动的对象要强制指定允许访问所有的链接
//        ChromeOptions options = new ChromeOptions();
//        options.addArguments("--remote-allow-origins=*");
//        WebDriver driver = new ChromeDriver(options);
//        WebDriverManager.edgedriver().setup();
//        EdgeOptions options = new EdgeOptions();
//        options.addArguments("--remote-allow-origins=*");
//        WebDriver driver = new EdgeDriver(options);
//        Thread.sleep(3000);
        //2输入完整的网址信息

        createDriver();
//        driver.get("https://www.baidu.com");


        //3找到输入框，并输入关键词
        driver.findElement(By.cssSelector("#kw")).sendKeys("范冰冰");
        Thread.sleep(3000);
        //4找到百度一下按钮，点击
        driver.findElement(By.cssSelector("#su")).click();
        Thread.sleep(3000);
        //5关闭浏览器
        driver.quit();




    }
    void tset2() throws InterruptedException, IOException {

        createDriver();
        // 你在这里写什么就传递的是什么
        getScreenShot(getClass().getName());
        Thread.sleep(3000);
        driver.findElement(By.cssSelector("#s-hotsearch-wrapper > div"));
        driver.quit();
    }
    void tset3() throws InterruptedException, IOException {

    }
    void getScreenShot(String str) throws InterruptedException, IOException {

        SimpleDateFormat sim1 = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sim2 = new SimpleDateFormat("HHmmssSS");
        String dirTime = sim1.format(System.currentTimeMillis());
        String fileTime = sim2.format(System.currentTimeMillis());
        System.out.println(dirTime);
        System.out.println(str);
        String filename = "./src/main/java/image/"+dirTime+"/"+str + "_" + fileTime + ".png";
        System.out.println("fileTime: " + filename);
        File srcFile=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(srcFile,new File(filename));
    }

}
